package de.openlaboratory.elib.view.beans;

import java.io.IOException;
import java.io.Serializable;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.NodeCollapseEvent;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.NodeUnselectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import de.openlaboratory.elib.core.LogManager;
import de.openlaboratory.elib.model.system.FileTreeDisplayNode;

@ManagedBean
@ViewScoped
public class FileImportBean implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7466567675690405367L;

	private final String DUMMY_NODE_TEXT = ".";
	
	private TreeNode root;
	private TreeNode selectedNode;

	@SuppressWarnings("unused")
	public FileImportBean()
	{
		root = new DefaultTreeNode("Root", null);

		loadRootNodes(root);

	}

	public TreeNode getRoot()
	{
		return root;
	}

	public TreeNode getSelectedNode()
	{
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode)
	{
		this.selectedNode = selectedNode;
	}

	public void onNodeExpand(NodeExpandEvent event)
	{
		TreeNode node = event.getTreeNode();
		
		// removeDummyNode(node);
		loadSubNodes(node);
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Expanded", event.getTreeNode().toString());

		// FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void onNodeCollapse(NodeCollapseEvent event)
	{
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Collapsed", event.getTreeNode().toString());

		// FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void onNodeSelect(NodeSelectEvent event)
	{
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", event.getTreeNode().toString());

		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void onNodeUnselect(NodeUnselectEvent event)
	{
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Unselected", event.getTreeNode().toString());

		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	private void loadRootNodes(TreeNode root)
	{
		Dictionary<String, Path> rootPaths = new Hashtable<String, Path>();

		rootPaths.put("Studium", Paths.get("C:\\Users\\xeiojt\\Desktop\\DATA\\My Box Files\\Studium"));
		rootPaths.put("eBooks", Paths.get("C:\\Users\\xeiojt\\Desktop\\DATA\\USB-Stick\\eBooks"));

		Enumeration<String> keys = rootPaths.keys();
		
		while (keys.hasMoreElements())
		{
			String key = (String) keys.nextElement();
			Path path = (Path) rootPaths.get(key);
			
			FileTreeDisplayNode fdn = new FileTreeDisplayNode(path, key);
			TreeNode dtn = new DefaultTreeNode(fdn, root);
			
			addDummyNode(dtn);
		}
	}

	private void loadSubNodes(TreeNode treeNode)
	{
		FileTreeDisplayNode parent = (FileTreeDisplayNode) treeNode.getData();
		Path path = parent.getFile();
		if (path.isAbsolute())
		{
			try (DirectoryStream<Path> ds = Files.newDirectoryStream(path))
			{
				for (Path p : ds)
				{
					// Iterate over the paths in the directory and print filenames
					FileTreeDisplayNode f = new FileTreeDisplayNode(p, p.getFileName().toString());
					TreeNode n = new DefaultTreeNode(f, treeNode);
					
					if(p.toFile().isDirectory())
						addDummyNode(n);
				}

			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}

	}

	private void addDummyNode(TreeNode n)
	{
		TreeNode dummy = new DefaultTreeNode(DUMMY_NODE_TEXT, n);
	}

	private void removeDummyNode(TreeNode node)
	{
		List<TreeNode> nodes = node.getChildren();
		
		for(TreeNode n : nodes)
		{
			if (n.toString().equals(DUMMY_NODE_TEXT))
			{
				nodes.remove(n);
			}
		}
	}
}
